package com.dzm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzm.entity.Emp;
import com.dzm.mapper.EmpMapper;
import com.dzm.service.IEmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dzm.vo.EmpVo;
import com.dzm.vo.SearchVo;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dzm
 * @since 2024-08-17
 */
@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<EmpVo> searchEmp(Integer pageNum, Integer pageSize, SearchVo searchVo) {
        Integer star = (pageNum-1)*pageSize;
        List<EmpVo> list = empMapper.searchEmp(star, pageSize, searchVo);
        return list;
    }

    @Override
    public EmpVo findById(String eid) {
        EmpVo emp = empMapper.findById(eid);
        return emp;
    }

    @Transactional
    @Override
    public void updateEmp(EmpVo empVo) {
        empMapper.updateEmp(empVo);
        empMapper.updateWork(empVo);
    }

    @Override
    public void addEmp(EmpVo empVo) {
        Integer empCount = empMapper.count() + 1;
        NumberFormat format = NumberFormat.getInstance();
        format.setMinimumIntegerDigits(5);
        String eid = "A" + format.format(empCount);
        eid = eid.replaceAll(",", "");

        empMapper.addEmp(eid, empVo);
        empMapper.addWork(eid, empVo);
    }

    @Override
    public Integer empCount(SearchVo searchVo) {
        Integer count = empMapper.useCount(searchVo);
        return count;
    }

    @Transactional
    @Override
    public void removeEmp(List<String> eids) {
        empMapper.removeEmp(eids);
        empMapper.removeWork(eids);
    }

    @Override
    public IPage<EmpVo> searchByWrapper(Integer pageNum, Integer pageSize, SearchVo vo) {
        QueryWrapper<EmpVo> wrapper = new QueryWrapper<>();
        if(vo != null) {
            wrapper.like(StrUtil.isNotBlank(vo.getEname()), "ename", vo.getEname())
                    .like(StrUtil.isNotBlank(vo.getCity()), "city", vo.getCity())
                    .ge(vo.getMinSalary() != null, "w.salary", vo.getMinSalary())
                    .le(vo.getMaxSalary() != null, "w.salary", vo.getMaxSalary())
                    .eq(StrUtil.isNotBlank(vo.getDid()) && !vo.getDid().equals(""), "d.did", vo.getDid());
        }
        IPage<EmpVo> page = empMapper.searchByWrapper(new Page<>(pageNum, pageSize), wrapper);

        return page;
    }
}
